/*
 * Copyright (C) 2019 Baidu, Inc. All Rights Reserved.
 */
package com.ghr.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期工具类
 *
 * @author wushanhu
 */
@Slf4j
public class DateUtil {

//    private static final Logger log = LoggerFactory.getLogger(DateUtil.class);

    /**
     * 1970-01-01到当前的秒级时间戳
     *
     * @return
     */
    public static String getElapsedSecond() {
        return String.valueOf(getTimestamp());
    }

    public static long getTimestamp() {
        return (System.currentTimeMillis() / 1000);
    }

    /**
     * 时间戳转换成日期
     *
     * @param timestamp  时间戳
     * @param dateFormat 日期格式
     * @return
     */
    public static Date secondTimestampToDate(Long timestamp, SimpleDateFormat dateFormat) {
        try {
            if (timestamp == null) {
                return null;
            }
            Date date = new Date(timestamp * 1000L);
            String dateStr = dateFormat.format(date);
            return dateFormat.parse(dateStr);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return null;
        }
    }

    /**
     * 日期字符串转换成日期
     *
     * @param date
     * @param dateFormat
     * @return
     */
    public static Date toDate(String date, SimpleDateFormat dateFormat) {
        try {
            return dateFormat.parse(date);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return null;
        }
    }

    /**
     * 日期字符串转换成日期
     *
     * @param date
     * @param dateFormat
     * @return
     */
    public static Date toDate(String date, String dateFormat) {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
            return simpleDateFormat.parse(date);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return null;
        }
    }

    /**
     * 日期类型转换成日期字符串
     *
     * @param date
     * @param format
     * @return
     */
    public static String toDateStr(Date date, String format) {
        String result = StringUtils.EMPTY;
        try {
            if (date == null) {
                return result;
            }
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
            result = simpleDateFormat.format(date);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }
        return result;
    }

    /**
     * 日期字符串转换成日期
     *
     * @param date
     * @param dateFormat
     * @return
     */
    public static Date formatDate(Date date, SimpleDateFormat dateFormat) {
        try {
            String dateStr = dateFormat.format(date);
            return dateFormat.parse(dateStr);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return null;
        }
    }

    /**
     * 日期字符串转换成日期
     *
     * @param date
     * @param dateFormat
     * @return
     */
    public static Date formatDate(Date date, String dateFormat) {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
            String dateStr = simpleDateFormat.format(date);
            return simpleDateFormat.parse(dateStr);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return null;
        }
    }

    /**
     * 计算两个日期之间间隔的秒数
     *
     * @param startDate 开始日期
     * @param endDate   结束日期
     * @return
     */
    public static long intervalSeconds(Date startDate, Date endDate) {
        long intervalSecond = 0;
        try {
            if (startDate != null && endDate != null) {
                intervalSecond = (endDate.getTime() - startDate.getTime()) / 1000;
            }
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }
        return intervalSecond;
    }

    /**
     * 比较两个日期的大小
     *
     * @param dateA 日期A
     * @param dateB 日期B
     * @return
     */
    public static int compare(Date dateA, Date dateB) {
        int result = dateA.compareTo(dateB);
        return result;
    }

    /**
     * 获取指定的日期
     *
     * @param date     日期参数
     * @param dayCount 天数
     * @return
     */
    public static Date getDate(Date date, int dayCount) {
        Calendar theCa = Calendar.getInstance();
        theCa.setTime(date);
        theCa.add(Calendar.DATE, dayCount);
        Date start = theCa.getTime();
        return start;
    }

    /**
     * 获取指定的日期
     *
     * @param date        日期参数
     * @param dayCount    天数
     * @param hourCount   小时数
     * @param minuteCount 分钟数
     * @param secondCount 秒数
     * @return
     */
    public static Date getDate(Date date, int dayCount, int hourCount, int minuteCount, int secondCount) {
        Calendar theCa = Calendar.getInstance();
        theCa.setTime(date);
        theCa.add(Calendar.DATE, dayCount);
        theCa.add(Calendar.HOUR, hourCount);
        theCa.add(Calendar.MINUTE, minuteCount);
        theCa.add(Calendar.SECOND, secondCount);
        Date start = theCa.getTime();
        return start;
    }

    /**
     * 获取零点日期
     *
     * @param date 日期参数
     * @return 零点日期
     */
    public static Date getDateOfTheZero(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date zeroDate = calendar.getTime();
        return zeroDate;
    }

    /**
     * 获取一个月前的日期
     *
     * @return
     */
    public static Date getLast30Days() {
        return getDate(new Date(), -30);
    }

    /**
     * 判断两个时间是否相隔指定天数
     *
     * @param date1 日期1
     * @param date2 日期2
     * @param days  天数
     * @return true or false
     */
    public static boolean overSpecifiedDay(Date date1, Date date2, int days) {
        Date startTime = date1;
        Date endTime = date2;
        long between = endTime.getTime() - startTime.getTime();
        if (between > (days * 24 * 3600000)) {
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        try {
            String res;
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            long lt = 1587541991 * 1000;
            Date date = new Date(lt);
            res = simpleDateFormat.format(date);
            log.info(res);

            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
            Date tmpDate = dateFormat.parse("2020-04-29T15:42:06.510Z");
            log.info("utc date:{}", simpleDateFormat.format(tmpDate));
        } catch (Exception ex) {
            System.out.println(ex);
        }
    }

    /**
     * 获取当前时间
     *
     * @param dateFormatStr 时间格式
     * @return
     */
    public static String getDateStr(String dateFormatStr, Date date) {
        if (date == null) {
            return "";
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormatStr);
        return simpleDateFormat.format(date);
    }
}
